package cn.tedu.tea.adapter.controller;

import cn.tedu.tea.adapter.common.response.ResultVO;
import cn.tedu.tea.domain.bo.ContentBO;
import cn.tedu.tea.domain.service.ContentService;
import cn.tedu.tea.protocol.param.ContentParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/content")
@Api(tags = "01. 内容管理模块")
public class ContentController {
    @Autowired
    private ContentService contentService;

    public ContentController() {
        log.debug("创建控制器对象：ContentController");
    }

    @ApiOperation("添加内容")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public ResultVO insert(@Validated @RequestBody ContentParam contentParam){
        contentService.insert(contentParam);
        return new ResultVO("内容添加成功");
    }

    @ApiOperation("根据id删除内容")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public ResultVO deleteById(@PathVariable Integer id){
        contentService.deleteById(id);
        return new ResultVO("删除成功");
    }

    @ApiOperation("修改内容详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public ResultVO update(@Validated @RequestBody ContentParam content){
        contentService.modifyContent(content);
        return new ResultVO("修改成功");
    }

    @ApiOperation("根据id查询内容详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public ResultVO selectById(@PathVariable Integer id){
        ContentBO content = contentService.selectById(id);
        return new ResultVO(content);
    }

    @ApiOperation("查询内容列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public ResultVO selectAll(){
        List<ContentBO> list = contentService.selectAll();
        list.forEach(contentBO-> System.out.println(contentBO));
        return new ResultVO(list);
    }
}
